package com.qf.controller;

import com.qf.entity.Order;
import com.qf.entity.User;
import com.qf.service.OrderService;
import com.qf.service.UserService;
import com.qf.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
public class RegisterController {
    @Autowired
    private UserService userService;
    @Autowired
    private OrderService orderService;

    @PostMapping("/register")
    public R register(@RequestBody User user) {
        // 检查用户名是否已存在
        User existUser = userService.findByUsername(user.getUsername());
        if (existUser != null) {
            return R.fail("用户名已存在");
        }
        userService.register(user);
        return R.ok();
    }
    @PostMapping("/addOrder")
    public R addOrder(
            @RequestParam("productName") String productName,
            @RequestParam("quantity") Integer quantity,
            @RequestParam("price") Double price,
            @RequestParam("userId") Integer userId,
            @RequestParam("createTime") String createTime) {

        orderService.addOrder(productName, quantity, price, userId, createTime);
        return R.ok("商品添加成功");
    }
}